首页 > 试题广场 >

求最大连续bit数

[编程题]求最大连续bit数
  • 热度指数:137524 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

求一个int类型数字对应的二进制数字中1的最大连续数,例如3的二进制为00000011,最大连续2个1

数据范围:数据组数:
进阶:时间复杂度:,空间复杂度:

输入描述:

输入一个int类型数字



输出描述:

输出转成二进制之后连续1的个数

示例1

输入

200

输出

2

说明

200的二进制表示是11001000,最多有2个连续的1。  
# 怎么快捷怎么来
def getl(n):
    num = bin(n)[2:].split('0')
    return max([len(n) for n in num if n !=''])
发表于 2023-09-09 23:21:35 回复(0)
#笨方法
n = int(input())
l = []
while n // 2:
if n%2 == 1:
l.append(1)
else:
l.append(0)
n = n//2
l.append(1)
l = l[::-1]
max = 1
for i in range(len(l)):
if l[i] == 1:
for j in range(i+1,len(l)):
if l[j]== 1:
if j-i+1 > max:
max = j-i+1
else:
break
print(max)
发表于 2023-02-22 16:35:07 回复(0)
s = bin(int(input()))[2:]
l = list(map(len,s.split('0')))
print(max(l))

发表于 2023-02-21 08:03:17 回复(0)
while 1:
    try:
        a= int(input())
        l = 0
        while a != 0:
            a &= a<<1
            l += 1
        print(l)
    except:
        break
发表于 2023-01-06 17:51:02 回复(0)
num = input()
bin_num = bin(int(num))
flag = 0
le = 0
temp = 0
for s in bin_num:
    if s == "1":
        flag = 1
    else:
        flag = 0
        temp = 0
    if flag == 1:
        temp+=1
    if temp>le:
        le = temp
    
print(le)   
发表于 2022-09-14 20:16:02 回复(0)
把二进制数转化为字符串,以0为分隔符分开,再倒序排列取第一个值计算长度,就是答案了
while True:
    try:
        print(len(sorted(str(bin(int(input())))[2:].split('0'),reverse=True)[0]))
    except:
        break


发表于 2022-09-10 14:13:11 回复(0)
while True:
    try:
        n = int(input())
        b = bin(n)[2::]
        str1 = b.rjust(8,'0')
        lst = []
        for i in range(len(str1)):
            if str1[i] == '0':
                continue
            else:
                for j in range(i+1,len(str1)):
                    if str1[i:j+1] == "1"*(j-i+1):
                        lst.append(j-i+1)
        if not lst:
            if n == 0:
                print("0")
            else:
                print("1")
        else:
            print(max(lst))
    except:
        break
发表于 2022-09-03 07:20:17 回复(0)
a=int(input())
b=bin(a)
x=str(b[2:]).replace('0',' ')
y=x.split()
z=sorted(y)
m=z[-1]
print(len(m))
发表于 2022-08-30 02:30:58 回复(0)

input_str = input()
input_2_str = bin(int(input_str))
input_2_str = input_2_str[2:]

longgest = 0
temp = 0
for i in input_2_str:
    if(i == "1"):
        temp = temp +1
    else:
        if(temp != 0):
            if(temp > longgest):
                longgest = temp
            temp = 0
        else:
            continue
#这里注意一个边界条件,就是如果最后一直都是1,那么最后不会触发else的情况,导致无法更新longgest
#所以循环结束后,还需要再判断一次是否保存
if(temp > longgest):
    longgest = temp
print(longgest)


发表于 2022-08-21 12:42:47 回复(0)
#法一:
n = bin(int(input()))[2:].split('0')
print(len(sorted(n)[-1]))
'''
法二:
temp = 0
lenth = 0
for i in n:
    if i == '1':
        temp += 1 
    else:
        temp = 0
    lenth = max(lenth,temp)
print(lenth)
'''

发表于 2022-08-09 16:37:35 回复(0)
n=int(input())
n=bin(n)
l=(n[2:].split('0'))
count=[]
for i in l:
        count.append(len(i))
print(max(count))
发表于 2022-08-07 07:14:22 回复(0)
a=int(input())
a=bin(a)
a=str(a)
b=[]

for i in range (0,len(a)):
    cc=0
    for j in range(i+1, len(a)):
        if a[i]=='1':
            if a[j]=='1':
                cc=cc+1
            else:
                break
    b.append(cc+1)

if max(b)==0:
    print(1)
else:
    print(max(b))

发表于 2022-08-01 16:44:50 回复(0)
while True:
    try:
        n = int(input())
        bit = bin(n).replace("0b","") 
        a = list(bit.split("0"))
        a.sort()
        print(len(a[-1]))
    except:
        break
发表于 2022-07-31 16:54:09 回复(0)
天啊,没想到可以用0分割后再算。。。
s = bin(int(input()))[2:].split('0')
maxlen = 0
for i in s:
    if len(i)>maxlen:
        maxlen = len(i)
print(maxlen)

# s = bin(int(input()))[2:].split('0')
# print(s)
# print(max(len(i) for i in s))


发表于 2022-07-23 12:27:58 回复(0)
n = int(input())
str1 = bin(n)[2::]
count = 0
for i in range(len(str1)):
    if str1[i-count:i+1] == '1' * (count + 1):
        count += 1
if count:
    print(count)

发表于 2022-06-30 13:52:53 回复(0)
while True:
    try:
        data = int(input())
        data = bin(data)[2:]
        res = 0
        max_len = 0
        for x in data:
            if x=='1':
                res+=1
                max_len = max(max_len, res)
            else:
                res=0
        print(max(max_len, res))
    except:
        break

当存在位为1时,长度加1并保存,只要确保输出最大长度的1即可
发表于 2022-06-27 14:23:34 回复(0)
python3   :将输入转为二进制后,对二进制数据进行处理,用0切片,然后排序,取出最长一个元素的长度
while True:
    try:
        num = bin(int(input()))
        lis = list(num[2:].split('0'))
        lis.sort()
        print(len(lis[-1]))
    except:
        break

发表于 2022-06-26 15:24:37 回复(0)
def tm086():
    n=int(input())
    s=bin(n)
    s1=s[2::]
    s2=s1.replace('0',' ')
    lst=s2.split()
    count=[]
    for value in lst:
        count.append(len(value))
    
    fn=max(count)
    print(fn)

if  __name__=='__main__':
    tm086()
发表于 2022-06-13 22:56:28 回复(0)

问题信息

难度:
66条回答 28229浏览

热门推荐

通过挑战的用户

查看代码